package com.battle.service.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.battle.pojo.entity.User;
import com.battle.pojo.entity.UserShare;
import com.battle.pojo.param.userShare.*;
import com.battle.pojo.vo.userShare.*;
import com.battle.pojo.dto.userShare.*;
import com.battle.service.repository.in.UserRepository;
import com.battle.service.repository.in.UserShareRepository;
import com.battle.service.service.in.UserShareService;
import nirvana.core.domains.vo.PageResult;
import nirvana.core.utils.ids.IdGenerator;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 【描 述】：用户分享服务
 * 【环 境】：J2SE 17
 * 【详 细】：
 */
@Service
public class UserShareServiceImpl implements UserShareService {

    private final UserShareRepository userShareRepository;
    private final UserRepository userRepository;

    public UserShareServiceImpl(UserShareRepository userShareRepository, UserRepository userRepository) {
        this.userShareRepository = userShareRepository;
        this.userRepository = userRepository;
    }


    /**
     * 创建用户分享
     *
     * @param param
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createUserShare(CreateUserShareParam param) {
        for (Long receiveShareId : param.getReceiveShareId()) {
            UserShare userShare = new UserShare();
            BeanUtils.copyProperties(param, userShare);
            Long id = IdGenerator.SNOW.generate();
            userShare.setId(id);
            userShare.setReceiveShareId(receiveShareId);
            userShareRepository.create(userShare);
        }
    }

    /**
     * 修改用户分享
     *
     * @param param
     */
    @Override
    public void editUserShare(EditUserShareParam param) {
        Long id = param.getId();
        UserShare userShare = userShareRepository.getByIdCheck(id);
        BeanUtils.copyProperties(param, userShare);
        userShareRepository.edit(userShare);
    }

    /**
     * 移除用户分享
     *
     * @param id
     */
    @Override
    public void removeUserShare(Long id) {
        userShareRepository.removeById(id);
    }

    /**
     * 启动/禁用用户分享
     *
     * @param id
     */
    @Override
    public void enabledUserShare(Long id) {
        UserShare userShare = userShareRepository.getByIdCheck(id);
        userShare.setIsDisable(!userShare.getIsDisable());
        userShareRepository.edit(userShare);
    }

    /**
     * 获取用户分享详情
     *
     * @param id
     * @return
     */
    @Override
    public GetUserShareVo getUserShare(Long id) {
        UserShare userShare = userShareRepository.getByIdCheck(id);
        GetUserShareVo vo = new GetUserShareVo();
        BeanUtils.copyProperties(userShare, vo);
        return vo;
    }

    /**
     * 获取用户分享 列表
     *
     * @param param
     * @return
     */
    @Override
    public PageResult<GetUserShareListVo> getUserShareList(GetUserShareListParam param) {

        SelectUserShareDto dto = new SelectUserShareDto();
        BeanUtils.copyProperties(param, dto);
        Page<GetUserShareListVo> page = userShareRepository.selectUserShare(dto);
        List<GetUserShareListVo> list = page.getRecords();

        if (CollectionUtil.isNotEmpty(list)) {
            List<Long> userIds = new ArrayList<>();

            list.forEach(v -> {
                userIds.add(v.getShareId());
                userIds.add(v.getReceiveShareId());
            });

            Map<Long, User> userMap = userRepository.getMap(userIds);
            for (GetUserShareListVo vo : list) {
                User initiator = userMap.get(vo.getShareId());
                if (initiator != null) {
                    vo.setShareName(initiator.getName());
                }
                User user = userMap.get(vo.getReceiveShareId());
                if (user != null) {
                    vo.setReceiveShareName(user.getName());
                }
            }
        }

        return new PageResult<>(list, page);

    }

    /**
     * 获取用户分享下拉列表
     *
     * @param param
     * @return
     */
    @Override
    public List<GetUserShareDownListVo> getUserShareDownList(GetUserShareDownListParam param) {
        UserShare po = new UserShare();
        if (param != null) {
            BeanUtils.copyProperties(param, po);
        }
        List<UserShare> userShareList = userShareRepository.getListByEntity(po);

        List<GetUserShareDownListVo> list = new ArrayList<>();
        for (UserShare userShare : userShareList) {
            GetUserShareDownListVo vo = new GetUserShareDownListVo();
            BeanUtils.copyProperties(userShare, vo);
            list.add(vo);
        }
        return list;
    }


}



